﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using htmlHelpers.DropDown;
using htmlHelpers.Link;
using htmlHelpers.Separator;

namespace htmlHelpers.Navigation
{
    public class SushiNavigationDropDownItemBuilder: ISushiComponentBuilder
    {
          #region CSS Classes
        #endregion
        #region Builder Properties
        public ViewContext ViewContext { get; private set; }
        private SushiNavigationDropDownItem Component { get; set; }
        #endregion

        #region Fluent Common Setters

        /// <summary>
        /// Set the name Attribute in client Html
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SushiNavigationDropDownItemBuilder SetLinkName(string name)
        {
            this.Component.Link.SetName(name);
            return this;
        }

        public SushiNavigationDropDownItemBuilder SetLinkId(String id)
        {
            //this.Component.Link.SetId(id);
            return this;
        }


        public SushiNavigationDropDownItemBuilder SetMenuValue(String value)
        {
            this.Component.Link.SetCaption(value);
            return this;
        }

        public SushiNavigationDropDownItemBuilder AddDropDownLinkElement(SushiLinkBuilder link)
        {
            this.Component.DropDownMenu.AddElement(link);
            return this;
        }

        public SushiNavigationDropDownItemBuilder AddSeparator()
        {
            this.Component.DropDownMenu.AddElement(new SushiSeparatorBuilder());
            return this;
        }

        public SushiNavigationDropDownItemBuilder AddDropDownHeaderElement(String headerText)
        {
            var header = new SushiNavigationItemHeaderBuilder(ViewContext);
            header.SetValue(headerText);
            this.Component.DropDownMenu.AddElement(header);
            return this;
        }


        //public SushiNavigationDropDownItemBuilder SetDropDownMenuElements(SushiDropDownBuilder dropDown)
        //{
        //    this.Component.DropDownMenu = dropDown;
        //    return this;
        //}

        #endregion
  
        #region Constructor
        public SushiNavigationDropDownItemBuilder(ViewContext view)
        {
            this.ViewContext = view;
            this.Component = new SushiNavigationDropDownItem();
            this.Component.Link = new SushiLinkBuilder(view);
            this.Component.NavigationItem = new SushiNavigationItemBuilder(view);
            this.Component.DropDownMenu = new SushiDropDownBuilder(view);
        }
        #endregion

        #region StringBuilders


        private String CreateSushiNavigationDropDownItem()
        {
            this.Component.NavigationItem.AddCssClass("dropdown");
            this.Component.Link.AddCssClass("dropdown-toggle");
            this.Component.Link.AddInnerHtml("<b class=\"caret\"></b>");
            this.Component.Link.AddHtmlAttribute("data-toggle", "dropdown");
            this.Component.NavigationItem.ContainerElements.Add(this.Component.Link);
            if (this.Component.DropDownMenu.ContainerElements.Count>0)
            {
                this.Component.NavigationItem.ContainerElements.Add(this.Component.DropDownMenu);
            }
            return this.Component.NavigationItem.ToString();
        }

        #endregion

        #region IHtmlString

        public override String ToString()
        {
            return CreateSushiNavigationDropDownItem();
        }

        public String ToHtmlString()
        {
            return this.ToString();
        }
        #endregion
    
    }
}
